home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.8 KB | 117 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 8.1 (Golden Search for a Minimum).
- % Section 8.1, Minimization of a Function, Page 413
- echo on; clc; format long; hold off; clear
-
- % This program finds the minimum of a function f(x).
-
- % The method is the golden ratio search.
-
- % It is assumed that f(x) is unimodal over [a,b].
-
- % The function f(x) is to be placed in the M-file f.m
- % function z = f(x)
- % z = x.^2 - sin(x);
-
- delete f.m
- diary f.m; disp('function z = f(x)');...
- disp('z = x.^2 - sin(x);');...
- diary off;
-
- f(0); % Remark. f.m and golden.m are used for Algorithm 8.1
-
- pause % Press any key to continue.
-
- clc;
-
- % Place the endpoints interval [a,b] in a and b.
-
- a = 0;
-
- b = 1;
-
- pause % Press any key to graph the function.
-
- clc; clg;
- hs = (b-a)/150;
- Xs = a:hs:b;
- Ys = f(Xs);
- axis([-0.05 1.05 -0.25 0.16]);...
- plot(Xs,Ys,'g');...
- hold on;...
- plot([-0.05 1.05],[0 0],'b',[0 0],[-0.25 0.16],'b');...
- xlabel('x');...
- ylabel('y');...
- title('To search for the minimum of y = f(x).');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc;
- % Place the endpoints interval [a,b] in a and b.
-
- % Place the tolerance for the abscissas in delta.
-
- % Place the tolerance for the ordinates in epsilon.
-
- a = 0;
-
- b = 1;
-
- delta = 1e-5;
-
- epsilon = 1E-7;
-
- [p,yp,dp,dy,A,B,C,D] = golden('f',a,b,delta,epsilon);
-
- pause % Press any key to find the minimum of f(x).
-
-
- clc; clg;
- hs = (b-a)/150;
- Xs = a:hs:b;
- Ys = f(Xs);
- axis([a b -0.25 0.175]);...
- X1 = A(1:7); Y1 = f(X1);...
- X2 = B(1:7); Y2 = f(X2);...
- X3 = C(1:7); Y3 = f(X3);...
- X4 = D(1:7); Y4 = f(X4);...
- plot(Xs,Ys,'g',X1,Y1,'or',X2,Y2,'or',X3,Y3,'or',X4,Y4,'or');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[-0.25 0.175],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The golden search for the minimum of y = f(x).');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- Mx1 = 'The results for the golden ratio search.';
- Mx2 = 'The solution is [p f(p)];';
- Mx3 = 'The abscissa p ';
- Mx4 = 'error bound for p';
- Mx5 = 'The minimum value f(p)';
- Mx6 = 'error bound for f(p)';
- clc,echo off,diary output,...
- disp(''),disp(Mx1),disp(''),disp(Mx2),disp(''),...
- disp(Mx3),disp(p),disp(Mx4),disp(['± ',num2str(abs(dp))]),disp(''),...
- disp(Mx5),disp(yp),disp(Mx6),disp(['± ',num2str(abs(dy))]),diary off,echo on
-
- pause % Press any key to see all the iterations.
-
- Z = [A;C;D;B]';
- Mx7 = 'The complete list of iterations:';
- Mx8 = ' a(k) d(x) d(k) b(k)';
- clc,echo off,diary output,format short,...
- disp(''),disp(Mx7),disp(''),disp(Mx8),disp(Z),diary off,echo on
-
-
-